package com.yulongtian.oneProblemEveryDay.month202211.day20221115;

import java.util.Arrays;
import java.util.Comparator;

/**
 * https://leetcode.cn/problems/maximum-units-on-a-truck/
 *
 * @author yulongTian
 * @create 2022-11-15 11:00
 */
public class Test01 {
    public static void main(String[] args) {
//        int[][] boxTypes = {{5, 10}, {2, 5}, {4, 7}, {3, 9}};
        int[][] boxTypes = {{1, 3}, {2, 2}, {3, 1}};
        int i = maximumUnits(boxTypes, 4);
        System.out.println(i);

    }

    public static int maximumUnits(int[][] boxTypes, int truckSize) {
        Arrays.sort(boxTypes, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[1] - o1[1];
            }
        });

        int restTruckSize = truckSize;
        int res = 0;
        for (int[] boxType : boxTypes) {
            if (restTruckSize >= boxType[0]) {
                restTruckSize -= boxType[0];
                res += boxType[0] * boxType[1];
            } else {
                res += restTruckSize * boxType[1];
                break;
            }
        }
        return res;
    }


}
